package com.dkm.orproject.dkmoa.action;

import com.alibaba.fastjson.JSON;
import com.dkm.orproject.dkmoa.domain.OaAsset;
import com.dkm.orproject.dkmoa.domain.OaDeliverFromGodown;
import com.dkm.orproject.dkmoa.domain.OaLog;
import com.dkm.orproject.dkmoa.domain.OaPersonnel;
import com.dkm.orproject.dkmoa.domain.vo.OaDeliverFromGodownVo;
import com.dkm.orproject.dkmoa.service.OaAssetService;
import com.dkm.orproject.dkmoa.service.OaDeliverFromGodownService;
import com.dkm.orproject.utils.jwt.login.UserLoginToken;
import com.dkm.orproject.utils.utils.Tools;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 *  @ClassName : OaDeliverFromGodownAction  //类名
 *  @Description : 资产出库  //描述
 *  @Author : 石智诚  //作者
 *  @Date: 2020-11-30 16:26  //时间
 */
@RestController
@RequestMapping("/OaDeliverFromGodownAction")
public class OaDeliverFromGodownAction {
    /**
     * 资产出库service
     */
    @Resource
    OaDeliverFromGodownService fromGodownService;
    /**
     * 资产总库
     */
    @Resource
    OaAssetService assetService;
    /**
     * 工具类
     */
    @Resource
    Tools tools;
    /**
     * 日志
     */
    OaLog log;
    /**
     * 登陆人
     */
    OaPersonnel person;

    /**
     * 回调map
     */
    Map<String,Object> map;
    /**
     * 行数
     */
    int row;

    /**
     * 查询所有资产的记录
     *
     * @param deliverFromGodown 资产出库记录
     * @param page              页数
     * @param limit             显示数量
     * @param startTime         开始时间
     * @param endTime           结束时间
     * @param request           请求
     * @return map
     */
    @UserLoginToken
    @GetMapping("deliverFromGodownFindAll")
    public Map<String, Object> findAll(OaDeliverFromGodownVo deliverFromGodown,
                                       Integer page,
                                       Integer limit,
                                       String startTime,
                                       String endTime, HttpServletRequest request) {
        try {
            List<OaDeliverFromGodownVo> all = fromGodownService.findAll(deliverFromGodown, startTime, endTime, page, limit);
            Long count = fromGodownService.rowCount(deliverFromGodown, startTime, endTime);
            return Tools.maps(all, count);
        } catch (Exception e) {
            addErrorLog(request, "资产出库记录查询", e);
        }
        return null;
    }
    /**
     * 增加或者更新资产出库表格
     * @param deliverFromGodown 资产出库记录
     * @param request 请求
     * @return map
     */
    @UserLoginToken
    @PostMapping("fromGodownAddOrUpdate")
    public  Map<String,Object> addOrUpdate(OaDeliverFromGodown deliverFromGodown,HttpServletRequest request){
      rowMap(request);
        if (ObjectUtils.isEmpty(person)) {
            tools.rowMap(map,0,log);
        }
        try {
            if(deliverFromGodown.getPkDeliverFromGodownId() != null && deliverFromGodown.getPkDeliverFromGodownId() > 0) {
                deliverFromGodown.setUpdatedBy(person.getPersonnelName());
                deliverFromGodown.setUpdatedTime(new Date());
                //资产出库更改前
                OaAsset oaAssetbefore = new OaAsset();
                oaAssetbefore.setPersonnelId(null);
                int update1 = assetService.updateById(oaAssetbefore);
                //资产出库更新以后的
                OaAsset oaAsset = new OaAsset();
                oaAsset.setPersonnelId(deliverFromGodown.getPersonnelId());
                int update = assetService.updateById(oaAsset);
                row = fromGodownService.updateByPrimaryKeySelective(deliverFromGodown);
                log.setLogTitle("资产出库更新记录");
            }else {
                deliverFromGodown.setCreatedBy(person.getPersonnelName());
                OaAsset oaAsset = new OaAsset();
                //资产的使用人改为
                oaAsset.setPersonnelId(deliverFromGodown.getPersonnelId());
                int update = assetService.updateById(oaAsset);
                row = fromGodownService.insert(deliverFromGodown);
                log.setLogTitle("资产出库增加记录");
            }
            log.setIsRevision("1");
            log.setLogBody(JSON.toJSON(deliverFromGodown).toString());
            tools.rowMap(map, row, log);
        } catch (Exception e) {
            addErrorLog(request,"资产出库增加或者更新",e);
        }

        return  map;
    }



    /**
     * 根据资产出库记录id查询
     * @param id 资产出库记录id
     * @param request 请求
     * @return map
     */
    @GetMapping("deliverFromGodownFindById/{id}")
    public  Map<String,Object>deliverFromGodownFindById(@PathVariable("id") Long id, HttpServletRequest request){
        rowMap(request);
        if (ObjectUtils.isEmpty(person)) {
            tools.rowMap(map,0,log);
        }
        try {
            OaDeliverFromGodown deliverFromGodown = fromGodownService.selectByPrimaryKey(id);
            map.put("data", deliverFromGodown);
        } catch (Exception e) {
            addErrorLog(request, "根据资产出库id查询", e);
        }
        return map;
    }

    /**
     * 批量删除所有
     * @param id 资产出库记录id 的集合
     * @param request 请求
     * @return map
     */
    @UserLoginToken
    @DeleteMapping("deliverFromGodownDeleteAll")
    public Map<String,Object> deliverFromGodownDeleteAll(@RequestParam("ids[]")Long[]id,HttpServletRequest request){
        rowMap(request);
        //判断登陆人是否为空
        if (ObjectUtils.isEmpty(person)) {
            tools.rowMap(map,0,log);
        }
        try {
            //将Long 类型的数组转化为list
            List<Long> list = Arrays.asList(id);
            //批量删除数据
            row= fromGodownService.deleteByAll(list);
            //日志标题
            log.setLogTitle("转正流程批量删除");
            //日志内容
            log.setLogBody(JSON.toJSON(row).toString());
            //日志类型
            log.setLogType("1");
            //新增日志信息格式化返回值给前台
            tools.rowMap(map,row,log);
        } catch (Exception e) {
            //收集错误的信息
            addErrorLog(request,"资产出库记录删除",e);
        }
        return map;
    }

    /**
     * 根据资产出库id删除
     * @param id 资产出库id
     * @param request 请求
     * @return map
     */
    @DeleteMapping("deliverFromGodownDeleteById/{id}")
    public Map<String,Object> deliverFromGodownDeleteById(@PathVariable("id") Long id,HttpServletRequest request){
        rowMap(request);
        if (ObjectUtils.isEmpty(person)) {
            tools.rowMap(map,0,log);
        }
        try {
            OaDeliverFromGodown oaDeliverFromGodown = fromGodownService.selectByPrimaryKey(id);
            row=fromGodownService.deleteByPrimaryKey(id);
            log.setLogTitle("资产出库记录删除");
            log.setLogBody(JSON.toJSON(oaDeliverFromGodown).toString());
            log.setIsRevision("1");
            tools.rowMap(map, row, log);
        } catch (Exception e) {
           addErrorLog(request,"资产出库记录删除",e);
        }
        return map;
    }

    private void addErrorLog(HttpServletRequest request,String title,Exception e){
        e.printStackTrace();
        OaPersonnel personnel= (OaPersonnel) request.getSession().getAttribute("user");
        log=new OaLog();
        log.setLogType("0");
        log.setLogBody(e.getLocalizedMessage());
        log.setLogTitle(title);
        log.setLogIp(request.getRemoteAddr());
        if(ObjectUtils.isEmpty(personnel)){
            log.setCreatedBy(personnel.getPersonnelName());
        }
        tools.errorMap(log);
    }

    /**
     * 简化代码
     * @param request 请求
     */
    private void rowMap(HttpServletRequest request){
        //新建返回Map模型
        map=new HashMap<>(16);
        //获取登陆人信息
        person= (OaPersonnel) request.getSession().getAttribute("user");
        //新建日志
        log=new OaLog();
        //得到ip
        log.setLogIp(request.getRemoteAddr());
        //登陆人名称
        log.setCreatedBy(person.getPersonnelName());
    }
}
